﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data;
using Telerik.Web.UI.GridExcelBuilder;
using System.Drawing;
using System.IO;

namespace Workflow.Alerts.Sublayouts
{
    public partial class InvoiceExcel : System.Web.UI.UserControl
    {
        string url, path = "";
        DataTable sourcetable;

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        public RadGrid InvoiceExcelGrid
        {
            get { return allInvoices; }
        }

        public void GetExcel()
        {
            allInvoices.MasterTableView.HierarchyLoadMode = GridChildLoadMode.ServerBind;
            allInvoices.ExportSettings.ExportOnlyData = true;
            allInvoices.ExportSettings.Excel.Format = GridExcelExportFormat.ExcelML;
            allInvoices.ExportSettings.IgnorePaging = true;            
            allInvoices.MasterTableView.ExportToExcel();
        }

        public void BindInvoiceReport(DataTable data)
        {
            sourcetable = data;
            DataRow[] alerts = sourcetable.Select("rowtype=0");
            allInvoices.DataSource = alerts;
            allInvoices.DataBind();
        }

        protected void allInvoices_DetailTableDataBind(object source, GridDetailTableDataBindEventArgs e)
        {
            Telerik.Web.UI.GridDataItem dataItem = (Telerik.Web.UI.GridDataItem)e.DetailTableView.ParentItem;
            switch (e.DetailTableView.Name)
            {
                case "dtlAlerts":
                    {
                        string noid = dataItem.GetDataKeyValue("networkoperatorid").ToString();
                        e.DetailTableView.DataSource = sourcetable.Select(string.Format("networkoperatorid={0} AND rowtype=1 ", noid));
                        break;
                    }
                case "dtlMaterials":
                    {
                        string alertid = dataItem.GetDataKeyValue("alertid").ToString();
                        e.DetailTableView.DataSource = sourcetable.Select(string.Format("alertid={0} AND rowtype=2 ", alertid));
                        break;
                    }
            }
        }

        protected void allInvoices_ExcelMLExportStylesCreated(object source, Telerik.Web.UI.GridExcelBuilder.GridExportExcelMLStyleCreatedArgs e)
        {
            foreach (StyleElement style in e.Styles)
            {
                switch (style.Id)
                {
                    case "headerStyle":
                        style.InteriorStyle.Color = Color.Gray;
                        style.FontStyle.Color = Color.Black;
                        style.FontStyle.Bold = true;
                        break;
                }
            }
        }

        protected void allInvoices_GridExporting(object source, GridExportingArgs e)
        {
            url = String.Format("~/Invoices{0}_{1}.xls", (Session[WorkflowConstants.CurrentUser] as User).MunicipalityId, DateTime.Now.ToString("MMddhh"));
            path = Server.MapPath(url);

            if (File.Exists(path))
            {
                File.Delete(path);
            }

            using (FileStream fs = File.Create(path))
            {
                Byte[] info = System.Text.Encoding.Default.GetBytes(e.ExportOutput);
                fs.Write(info, 0, info.Length);
            }
            Session["invoiceexport"] = "true";
            Response.Redirect(Sitecore.Links.LinkManager.GetItemUrl(Sitecore.Context.Item));
        }
    }
}